package com.weibo.demos.web.Controller;


import com.weibo.demos.web.Mapper.WeiboMapper;
import com.weibo.demos.web.Service.WeiboService;
import com.weibo.demos.web.entity.UserVO;
import com.weibo.demos.web.entity.WeiboDTO;
import com.weibo.demos.web.entity.WeiboDetailVO;
import com.weibo.demos.web.entity.WeiboVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
@Slf4j
public class WeiBoController {

    @Autowired
    private WeiboMapper weiboMapper;
    @Autowired
    private WeiboService weiboService;

    @PostMapping("/insert")
    public int insert(@RequestBody WeiboDTO weibo, HttpServletRequest request){
        //先从session中获取用户信息
        UserVO user = (UserVO) request.getSession().getAttribute("user");
        if (user==null){
            return 2;
        }
        //获取到当前的用户id
        int userId = user.getId();
        weibo.setUserId(userId);
        //根据当前用户id把发布的微博保存到数据库中
        weiboMapper.insert(weibo);
        return 1;
    }

    @GetMapping("/selectIndex")
    public List<WeiboVO> selectIndex(){
        log.info("查询首页");
        List<WeiboVO> weibovo=weiboService.selectIndex();
        return weibovo;
    }


    @GetMapping("/selectById")
    public WeiboDetailVO selectById(@RequestParam Integer id){
        log.info("根据id{}查询微博详情",id);
        return weiboMapper.selectById(id);
    }

}
